﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;

namespace FisheryPlatform.Common
{
    public class ImageUtility
    {
        /// <summary>
        /// 获取联系电话图片
        /// </summary>
        /// <param name="phoneString">phone,...</param>
        /// <returns></returns>
        public static byte[] ContactPhone(string phoneString)
        {
            #region
            Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.DarkBlue };
            Random rdm = new Random();
            System.Drawing.Brush brush = new System.Drawing.SolidBrush(Color.Red);
            System.Drawing.Pen pen = new Pen(brush);
            Graphics gh = null;
            Bitmap img = null;
            try
            {
                var data = (phoneString).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                //data += "," + data;
                var width = (data.Length >= 2 ? 23 : 11) * 10;
                var groupCount = Convert.ToInt32(Math.Ceiling((decimal)data.Length / 2));
                img = new Bitmap(width, groupCount * 24);
                gh = Graphics.FromImage(img);
                gh.Clear(Color.Beige);

                if (data.Length > 2)
                {
                    for (var i = 1; i <= groupCount; i++)
                    {
                        gh.DrawString(string.Format("{0},{1}", data[0], data[1]), new Font("Arial", 13, FontStyle.Bold), brush, new PointF(0, 3));
                    }

                    gh.DrawString(data[2], new Font("Arial", 13, FontStyle.Bold), brush, new PointF(0, 24));
                }
                else
                {
                    gh.DrawString(string.Join(",", data), new Font("Arial", 13, FontStyle.Bold), brush, new PointF(0, 3));
                }
                for (int i = 0; i < 100; i++)
                {
                    int num = rdm.Next(0, color.Length);
                    int x = rdm.Next(0, img.Width);
                    int y = rdm.Next(0, img.Height);
                    img.SetPixel(x, y, color[num]);
                }

                System.IO.MemoryStream ms = new MemoryStream();
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] buffer = new byte[ms.Length];
                buffer = ms.ToArray();
                ms.Close();
                return buffer;

                //img.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            catch (Exception ex)
            { }
            finally
            {
                if (gh != null)
                    gh.Dispose();

                if (img != null)
                    img.Dispose();
            }
            return new byte[] { };
            #endregion
        }

        /// <summary>
        /// 生成验证码图片
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static byte[] ValidateCode(string value)
        {
            #region
            Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.DarkBlue };
            Random rdm = new Random();
            System.Drawing.Brush brush = new System.Drawing.SolidBrush(Color.Red);
            System.Drawing.Pen pen = new Pen(brush);
            Graphics gh = null;
            Bitmap img = null;
            try
            {                
                img = new Bitmap(80, 24);
                gh = Graphics.FromImage(img);
                gh.Clear(Color.Beige);
                gh.DrawString(value, new Font("Arial", 13, FontStyle.Bold), brush, new PointF(15, 3));
                for (int i = 0; i < 100; i++)
                {
                    int num = rdm.Next(0, color.Length);
                    int x = rdm.Next(0, img.Width);
                    int y = rdm.Next(0, img.Height);
                    img.SetPixel(x, y, color[num]);
                }
                System.IO.MemoryStream ms = new MemoryStream();
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] buffer = new byte[ms.Length];
                buffer = ms.ToArray();
                ms.Close();

                return buffer;
            }
            catch (Exception exp)
            {
                throw new Exception(exp.ToString());
            }
            finally
            {
                if (gh != null)
                    gh.Dispose();

                if (img != null)
                    img.Dispose();
            }
            return new byte[] { };
            #endregion
        }
    }
}
